#include <QtTest/QtTest>
#include <QtHelp/QHelpEngineCore>

class QtWordFetch_test: public QObject
{
    Q_OBJECT
private slots:
    void getStringsFromFile_slot();

private slots:
    void lemmatize_finish(int exitCode, QProcess::ExitStatus exitStatus);
private:
    void first();
    void applicationDirPath();
    void splitBigFile();
    void lemmatize();
    void lemmatizeDir();
    void Transform(QString exeName, QString input, QString output, QString lang);
    int GetStringsFromFile(QString fileName);
private:
    QProcess process;
    QMap<QString,int> wordMap;
};

void QtWordFetch_test::first()
{
    QCOMPARE(true, true);
}

void QtWordFetch_test::applicationDirPath()
{
    QString currentPath = QCoreApplication::applicationDirPath();
    QCOMPARE("E:/Project/QtWordFetch/bin", currentPath);
}

const int MaxFileSize = 1000000;

void QtWordFetch_test::splitBigFile()
{
    QDir dir;
    QString currentPath = QCoreApplication::applicationDirPath();
    dir.setPath(currentPath+"/input/");

    QStringList filters;
    filters << "*.txt";
    QStringList files = QDir(dir).entryList(filters);

    for(int i = 0; i < files.size(); i++)
    {
        QString fileName = files[i];
        QString absoluteName = currentPath+"/input/"+fileName;
        QFile Bigfile(absoluteName);
        if (!Bigfile.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            continue;
        }
        if(Bigfile.size() > MaxFileSize)
        {
            for(int i = 0; i < Bigfile.size()/MaxFileSize + 1; i++)
            {
                QString newSmalFileName = currentPath+"/input/"+QString("%1").arg(i)+fileName;
                QByteArray writeData = Bigfile.read(MaxFileSize);
                if(writeData.size() > 0)
                {
                    QFile smallFile(newSmalFileName);
                    if (!smallFile.open(QIODevice::ReadWrite | QIODevice::Text))
                    {
                        continue;
                    }
                    smallFile.write(writeData);
                    smallFile.close();
                }
            }
            Bigfile.close();
            Bigfile.remove();
            continue;
        }
        Bigfile.close();
    }
    for(int i = 0; i < files.size(); i++)
    {
        QString fileName = files[i];

        QFile Bigfile(currentPath+"/input/"+fileName);
        if (!Bigfile.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            continue;
        }
        if(Bigfile.size() > MaxFileSize)
        {
            QCOMPARE(true, false);
        }
        Bigfile.close();
    }
}

void QtWordFetch_test::Transform(QString exeName, QString input, QString output, QString lang)
{
    QStringList arguments;
    arguments << "-l" << lang << input << output << "-v";

    QString currentPath = QCoreApplication::applicationDirPath();
    QString filePath = currentPath +"\\"+ exeName;
    qDebug() << QDir::currentPath();

    connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(lemmatize_finish(int, QProcess::ExitStatus)));
    process.start(filePath, arguments);
    if (!process.waitForFinished())
    {
        QCOMPARE(10, 1000);
    }
    else
    {
        QCOMPARE(true, true);
    }
}

void QtWordFetch_test::lemmatize()
{
    Transform("lemmatize.exe","input.txt", "output.txt", "lem-me-en.bin");
}

void QtWordFetch_test::lemmatizeDir()
{
//    QDir dir;
//    QString currentPath = QCoreApplication::applicationDirPath();
//    dir.setPath(currentPath+"/input/");

//    QStringList filters;
//    filters << "*.txt";
//    QStringList files = QDir(dir).entryList(filters);

//    for(int i = 0; i < files.size(); i++)
//    {
//        QString fileName = files[i];
//        Transform("lemmatize.exe",currentPath+"/input/"+fileName, currentPath + "/output/" + fileName, "lem-me-en.bin");
//    }
}

int QtWordFetch_test::GetStringsFromFile(QString fileName)
{
    QFile file(fileName);

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return 0;
    }

    QString allFileData;
    allFileData.append(file.readAll());
    int index = 0;
    QRegularExpression regularExpression("\\b\\w+\\b");
    QRegularExpressionMatch match;
    do
    {
        match = regularExpression.match(allFileData, index);
        if(match.hasMatch())
        {
            index = match.capturedEnd();
            //list << match.captured(0);

            if(wordMap.find(match.captured(0)) == wordMap.end())
            {
                wordMap[match.captured(0)] =0;
            }
        }
        else
            break;
    }
    while(index < allFileData.length());

    file.close();
    return wordMap.size();
}

void QtWordFetch_test::getStringsFromFile_slot()
{
    QDir dir;
    QStringList list;
    QString currentPath = QCoreApplication::applicationDirPath();
    dir.setPath(currentPath+"/output/");

    QStringList filters;
    filters << "*.txt";
    QStringList files = QDir(dir).entryList(filters);
    for(int i = 0; i < files.size(); i++)
    {
        int size = GetStringsFromFile(currentPath+"/output/"+files[i]);
        qDebug() << size << currentPath+"/output/"+files[i];

        QCOMPARE(true, true);
    }
    QFile file(currentPath+"/outputFilter/"+"file.data");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);   // we will serialize the data into the file
    out << wordMap; //
    wordMap.clear();
    file.close();
    QCOMPARE(true, true);
}

void QtWordFetch_test::lemmatize_finish(int exitCode, QProcess::ExitStatus exitStatus)
{
    QCOMPARE(0, exitCode);
    QCOMPARE(QProcess::NormalExit, exitStatus);
}



QTEST_MAIN(QtWordFetch_test)
#include "QtWordFetch_test.moc"

